package cn.good.yan.handle;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 重写，登录失败后，跳转的请求 - 调整到外部地址
 * 输入地址，http://localhost:8081/login.html
 * 进行登录，用户admin 密码123 ，输入正确，跳转百度
 * 输入错误，进入 MyAuthenticaFailtHandler 里的逻辑
 *
 * @author shengren.yan
 * @create 2024/9/26
 */
public class MyAuthenticaFailtHandler implements AuthenticationFailureHandler {


    private String url;

    // 构造函数
    public MyAuthenticaFailtHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse response,
                                        AuthenticationException e) throws IOException, ServletException {

        // 重写跳转到该地址（url）
        response.sendRedirect(url);

    }

}
